package net.lijingye.education.controller;

import net.lijingye.education.entity.*;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.response.meta.ObjectNotExistsMeta;
import net.lijingye.education.service.LessonService;
import net.lijingye.education.service.UserService;
import net.lijingye.education.service.UtilService;
import net.lijingye.education.util.CommonUtil;
import net.lijingye.education.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LijingYe on 2016/3/27.
 */
@RestController
public class LessonController extends BaseController {
    @Autowired
    private LessonService lessonService;
    @Autowired
    private UserService userService;
    @Autowired
    private UtilService utilService;

    @RequestMapping(value = "/lessons/{lessonId}", method = RequestMethod.GET)
    public BaseResponse getLesson(@PathVariable String lessonId) throws DaoException {
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            LessonVO lessonVO = new LessonVO(lesson);
            return new SuccessResponse(lessonVO);
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/lessons/{lessonId}/students", method = RequestMethod.GET)
    public BaseResponse getLessonStudents(@PathVariable String lessonId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            List<User> studentList = lesson.getStudentList();
            List<StudentVO> studentVOList = new ArrayList<>();
            studentList.forEach(student -> {
                studentVOList.add(new StudentVO(student));
            });
            return CommonUtil.splitPage(studentVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/lessons/{lessonId}/topics", method = RequestMethod.GET)
    public BaseResponse getLessonTopics(@PathVariable String lessonId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            List<Topic> topicList = lesson.getTopicList();
            List<TopicLiteVO> topicLiteVOList = new ArrayList<>();
            topicList.forEach(topic -> {
                topicLiteVOList.add(new TopicLiteVO(topic));
            });
            return CommonUtil.splitPage(topicLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/lessons/{lessonId}/assignments", method = RequestMethod.GET)
    public BaseResponse getLessonAssignments(@RequestHeader("Authorization") String accessToken, @PathVariable String lessonId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            User user = userService.getUserByAccessToken(accessToken);
            Lesson lesson = lessonService.getLessonById(lessonId);
            List<Assignment> assignmentList = lesson.getAssignmentList();
            List<AssignmentLiteVO> assignmentLiteVOList = new ArrayList<>();
            for (Assignment assignment : assignmentList) {
                StudentAssignment studentAssignment = utilService.getStudentAssignmentById(assignment.getId(), user.getId());
                assignmentLiteVOList.add(new AssignmentLiteVO(assignment, studentAssignment));
            }
            return CommonUtil.splitPage(assignmentLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/lessons/{lessonId}/sources", method = RequestMethod.GET)
    public BaseResponse getLessonSources(@PathVariable String lessonId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            List<Source> sourceList = lesson.getSourceList();
            List<SourceLiteVO> sourceLiteVOList = new ArrayList<>();
            sourceList.forEach(source -> {
                sourceLiteVOList.add(new SourceLiteVO(source));
            });
            return CommonUtil.splitPage(sourceLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(value = "/lessons/{lessonId}/ppts", method = RequestMethod.GET)
    public BaseResponse getLessonPPTs(@PathVariable String lessonId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            List<PPT> pptList = lesson.getPPTList();
            List<PPTLiteVO> pptLiteVOList = new ArrayList<>();
            pptList.forEach(ppt -> {
                pptLiteVOList.add(new PPTLiteVO(ppt));
            });
            return CommonUtil.splitPage(pptLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }
}
